package com.example.movie.ui.home.tabs

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.SimpleAdapter
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.bumptech.glide.Glide
import com.example.movie.R
import com.example.movie.common.constants.AppConstants
import com.example.movie.data.movie.factory.HomeViewModelFactory
import com.example.movie.data.movie.model.HomeViewModel
import com.example.movie.ui.movie.MovieDetailActivity
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton
import kotlinx.android.synthetic.main.fragment_top.*

class TopFragment : Fragment() {

    private val homeViewModel: HomeViewModel by viewModels(factoryProducer = { HomeViewModelFactory() })

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        initObserver()
        return inflater.inflate(R.layout.fragment_top, container, false)
    }

    private fun initObserver() {
        homeViewModel.loadMovieTopList()
        homeViewModel.topList.observe(viewLifecycleOwner) {
            val data = mutableListOf<Map<String, *>>()
            for (i in it.indices) {
                it[i].apply {
                    data.add(
                        mapOf(
                            Pair("seq", i + 1),
                            Pair("top_movie_img", cimg),
                            Pair("top_movie_name", movieName),
                            Pair("top_actors", areaName),
                            Pair("top_play_time", registerTime),
                            Pair("top_score", score),
                        )
                    )
                }
            }

            val simpleAdapter = SimpleAdapter(
                context, data, R.layout.item_top_list,
                arrayOf(
                    "seq",
                    "top_movie_img",
                    "top_movie_name",
                    "top_actors",
                    "top_play_time",
                    "top_score"
                ),
                intArrayOf(
                    R.id.seq,
                    R.id.top_movie_img,
                    R.id.top_movie_name,
                    R.id.top_actors,
                    R.id.top_play_time,
                    R.id.top_score
                )
            )
            //绑定网络图片
            simpleAdapter.viewBinder =
                SimpleAdapter.ViewBinder { view, data, textRepresentation ->
                    when(view.id){
                        R.id.seq ->{
                            val sequence = view as QMUIRoundButton
                            val seq = (data as Int)
                            sequence.text = seq.toString()

                            when (seq) {
                                1 -> {
                                        sequence.setBackgroundColor(Color.YELLOW)
                                }
                                2 -> {
                                    sequence.setBackgroundColor(Color.LTGRAY)
                                }
                                3 -> {
                                    sequence.setBackgroundColor(Color.GREEN)
                                }
                                else -> {
                                    sequence.setBackgroundColor(Color.alpha(R.color.white))
                                }
                            }

                            return@ViewBinder  true
                        }
                        R.id.top_movie_img ->{
                            Glide
                                .with(requireContext())
                                .load(AppConstants.STATIC_SOURCE_URL + data)
                                .placeholder(R.mipmap.logo)
                                .error(R.mipmap.ic_launcher)
                                .into((view as ImageView))
                            return@ViewBinder true
                        }

                    }
                    return@ViewBinder false
                }

            items.adapter = simpleAdapter

            items.setOnItemClickListener { parent, view, position, id ->
                with(Bundle()){
                    putInt("movieId",it[position].movieId!!)
                    val intent = Intent(requireContext(), MovieDetailActivity::class.java)
                    intent.putExtras(this)
                    startActivity(intent)
                }
            }
        }

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

    }


}